package com.zjj.learn.designmodel.pattern.singleton.demo8;

import java.io.Serializable;

/** 通过反射破坏单例模式
 * *** @author zjj_admin
 * *** 2022/4/5 16:08
 */
public class Singleton implements Serializable {

    private static boolean flag = false;

    private Singleton(){
        synchronized (Singleton.class){
            if(flag){
                throw new RuntimeException("Singleton 不能创建多个对象");
            }
            flag = true;
        }

    }

    //定义一个静态内部类，内部定义当前类的静态属性
    private static class SingletonHolder{
        private static final Singleton instance = new Singleton();
    }

    public static Singleton getInstance(){

        return SingletonHolder.instance;
    }
}
